/*************************************************************************
	> File Name: 012.P1616.疯狂的采药.cpp
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 二  6/29 15:10:33 2021
 ************************************************************************/

#include <iostream>
using namespace std;

long long t, m, a[10005], b[10005], dp[10000005];

int main() {
    cin >> t >> m;
    for (long long i = 1; i <= m; i++) {
        cin >> a[i] >> b[i];
    }

    for (long long i = 1; i <= m; i++) {
        for (long long j = a[i]; j <= t; j++) {
            dp[j] = max(dp[j], dp[j - a[i]] + b[i]);
        }
    }

    cout << dp[t] << endl;
    return 0;
}
